package com.ruicar.afs.cloud.basic.common.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.basic.common.entity.BasicExpenseRecord;
import com.ruicar.afs.cloud.basic.common.entity.BasicExpenseRecordDetail;
import com.ruicar.afs.cloud.basic.common.entity.BasicPenaltyInterestDetails;
import com.ruicar.afs.cloud.basic.common.mapper.BasicExpenseRecordDetailMapper;
import com.ruicar.afs.cloud.basic.common.service.BasicExpenseRecordDetailService;
import com.ruicar.afs.cloud.basic.common.service.BasicExpenseRecordService;
import com.ruicar.afs.cloud.basic.common.service.BasicPenaltyInterestDetailsService;
import com.ruicar.afs.cloud.basic.proceeds.debit.service.BasicDebitRecordService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.CostDetailedTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ExpenseStatusEnum;
import com.ruicar.afs.cloud.manage.api.dto.BasicExpenseRecordDetailDto;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author: Lee
 * @date 2020-08-10 04:59:50 
 * @description 
 */
@Service
@AllArgsConstructor
public class BasicExpenseRecordDetailServiceImpl extends ServiceImpl<BasicExpenseRecordDetailMapper, BasicExpenseRecordDetail> implements BasicExpenseRecordDetailService {

    private final BasicExpenseRecordService basicExpenseRecordService;
    private final BasicPenaltyInterestDetailsService basicPenaltyInterestDetailsService;
    private final BasicDebitRecordService basicDebitRecordService;


    @Override
    @Transactional
    public IResponse costExemptApprovel(BasicExpenseRecordDetailDto condition, Date batchDate) {
        boolean result = false;
        boolean expenseResult = true;
        boolean detailResult = true;
        boolean penaltyResult = true;
        List<BasicExpenseRecordDetailDto> basicExpenseRecordDetailList = condition.getBasicExpenseRecordDetailList();
        //豁免费用审批通过逻辑
        //1.改basic_expense_record_detail中费用相关信息,改basic_expense_record中的数据
        if(basicExpenseRecordDetailList != null && basicExpenseRecordDetailList.size() > 0){
            for(BasicExpenseRecordDetailDto detail : basicExpenseRecordDetailList){
                BasicExpenseRecordDetail recordDetail = this.getById(detail.getId());
                //合同费用信息总表
                List<BasicExpenseRecord> basicExpenseRecords = basicExpenseRecordService.list(Wrappers.<BasicExpenseRecord>query().lambda()
                        .eq(BasicExpenseRecord::getCostType, detail.getCostType())
                        .eq(BasicExpenseRecord::getContractNo, detail.getContractNo())
                );
                BasicExpenseRecord expenseRecord = basicExpenseRecords.get(0);
                //修改已豁免金额
                recordDetail.setExemptedAmt(recordDetail.getExemptionAmt().add(detail.getExemptionAmt()));
                //累加
                expenseRecord.setExemptedAmt(expenseRecord.getExemptedAmt().add(detail.getExemptionAmt()));
                //修改豁免金额
                recordDetail.setExemptionAmt(recordDetail.getExemptedAmt().add(detail.getExemptionAmt()));
                expenseRecord.setExemptionAmt(expenseRecord.getExemptionAmt().add(detail.getExemptionAmt()));
                //修改剩余应收金额
                BigDecimal remainingAmt = recordDetail.getCostAmt().subtract(recordDetail.getExemptionAmt()).subtract(recordDetail.getReceivedAmt());
                recordDetail.setRemainingAmt(remainingAmt);

                BigDecimal totalRemainingAmt = expenseRecord.getCostAmt().subtract(expenseRecord.getExemptionAmt()).subtract(expenseRecord.getReceivedAmt());
                expenseRecord.setRemainingAmt(totalRemainingAmt);
                //根据剩余应收金额判断是否改状态
                if(remainingAmt.compareTo(BigDecimal.ZERO) <= 0){
                    recordDetail.setStatus(ExpenseStatusEnum.settle);
                }else{
                    recordDetail.setStatus(ExpenseStatusEnum.unsettle);
                }
                if(totalRemainingAmt.compareTo(BigDecimal.ZERO) <= 0){
                    expenseRecord.setStatus(ExpenseStatusEnum.settle);
                }else{
                    expenseRecord.setStatus(ExpenseStatusEnum.unsettle);
                }
                detailResult = this.updateById(recordDetail);
                expenseResult = basicExpenseRecordService.updateById(expenseRecord);

                //2.在basic_penalty_interest_details中插入一条数据
                BasicPenaltyInterestDetails details = new BasicPenaltyInterestDetails();
                details.setContractNo(detail.getContractNo());
                details.setCostType(detail.getCostType());
                details.setOperateType(CostDetailedTypeEnum.arrearsExempt);
                details.setDate(new Date());
                details.setAmount(detail.getExemptionAmt());
                penaltyResult = basicPenaltyInterestDetailsService.save(details);
            }
            result = expenseResult &&  detailResult && penaltyResult;
            //重新生成扣款指令
            basicDebitRecordService.addDebitRecordList(basicExpenseRecordDetailList.get(0).getContractNo(),batchDate);
        }

        return IResponse.success(result);
    }
}
